package thread.communication;

import static java.lang.System.out;

/**
 * 
 * @author Shahid
 */
// Printing Hello world! 12 times with 1 sec Pause

public class InterruptTest {

	static Thread t = new Thread() {
		public void run() {

			try {
				for (int i = 1; i <= 12; i++) {
					out.println("Hello..." + i);
					Thread.sleep(1000);
				}
				out.println("<Not Interrupted>");
			} catch (InterruptedException e) {
				out.println("<Interrupted> " + e.toString());
			}
		}
	};

// ... }
	
	public static void main(String[] args) {

		t.start();
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			out.println("Error : " + e.toString());
		}

		t.interrupt();
		out.println("Main method finished");
	}
}

// Output without interrupt()
// Printing Hello world! 12 times with 1 sec Pause
/*
 * Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello,
 * world! Main method finished Hello, world! Hello, world! Hello, world! Hello,
 * world! Hello, world! Hello, world! Hello, world! Not Interrupted
 */

// Output with interrupt()
/*
 * Printing Hello world! 12 times with 1 sec Pause Hello, world! Hello, world!
 * Hello, world! Hello, world! Hello, world! Main method finished
 * java.lang.InterruptedException: sleep interrupted
 */

